package dao.impl;

import dao.UserDao;
import entity.User;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.Constants;
import util.JDBCUtils;

/**
 * 数据库访问实现类
 *
 * @version 1.0
 * @Author: xxf
 * @CreateDate: 2020/9/27 22:00
 */
public class UserDaoImpl implements UserDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 根据用户名查询用户是否存在
     *
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        User user = null;
        try {
            //定义sql
            String sql = "select u_id uid,u_name username,u_password upassword,u_email email,u_sex usex," +
                    "u_status ustatus,u_code ucode,u_role urole from user where u_name = ?";
//            String sql = "select * from user where u_name = ?";
            //执行sql
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username);
        } catch (DataAccessException e) {
            e.printStackTrace();
            System.out.println("findByUsername查询不到相同的username");
        }

        return user;
    }

    /**
     * 添加一个用户
     *
     * @param user
     * @return
     */
    @Override
    public int insertUser(User user) {
        String sql = "insert into user(u_name,u_password,u_email,u_sex,u_status,u_code,u_role) " +
                " values(?,?,?,?,?,?,?)";
        int rows = 0;
        rows = template.update(sql, user.getUsername(),
                user.getUpassword(),
                user.getEmail(),
                user.getUsex(),
                user.getUstatus(),
                user.getUcode(),
                user.getUrole());
        return rows;
    }

    /**
     * 查询激活码
     *
     * @param code
     * @return
     */
    @Override
    public User findByCode(String code) {
        System.out.println(code);
        User user = null;
        try {
            String sql = "select u_id uid,u_name username,u_password upassword,u_email email,u_sex usex," +
                    "u_status ustatus,u_code ucode,u_role urole from user where u_code = ?";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), code);
            System.out.println(user);
        } catch (Exception e) {
            System.out.println("查询不到激活码");
        }
        return user;
    }

    /**
     * 修改激活状态
     *
     * @param user
     */
    @Override
    public void updateStatus(User user) {
        String sql = "update user set u_status =" + Constants.ACTIVE_SUCCESS + " where u_id=?";
        template.update(sql, user.getUid());
    }

    /**
     * 根据用户名密码查询用户对象
     *
     * @param username
     * @param password
     * @return 返回一个用户对象
     */
    @Override
    public User findByUsernameAndPassword(String username, String password) {

        String sql = "select u_id uid,u_name username,u_password upassword,u_email email,u_sex usex," +
                "u_status ustatus,u_code ucode,u_role urole from user where u_name =? and u_password = ?";

        User user = null;
        try {
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
        } catch (Exception e) {
            System.out.println("findByUsernameAndPassword异常，查询失败，打印日志");
        }

        return user;
    }
}
